﻿$(document).ready(function () {
  startTheClock();
});

// http://stackoverflow.com/questions/5865089/simple-javascript-loop-that-repeats-each-second
function startTheClock() {
  core();
  setTimeout(startTheClock, 1000);
}

function core() {
  var binaryClock = new BinaryClock();
  var binaries = binaryClock.getBinaries(new Date());

  var prefixes = new Array('hh', 'hl', 'mh', 'ml', 'sh', 'sl');

  for (var i = 0; i < binaries.length; i++) {
    manageLeds(prefixes[i], binaries[i]);
  }
}

function manageLeds(prefix, binary) {
  // reset leds
  $('#' + prefix + '8').removeClass('ledOn');
  $('#' + prefix + '4').removeClass('ledOn');
  $('#' + prefix + '2').removeClass('ledOn');
  $('#' + prefix + '1').removeClass('ledOn');
  // set leds
  if (binary[0] == 1) {
    $('#' + prefix + '8').addClass('ledOn');
  }
  if (binary[1] == 1) {
    $('#' + prefix + '4').addClass('ledOn');
  }
  if (binary[2] == 1) {
    $('#' + prefix + '2').addClass('ledOn');
  }
  if (binary[3] == 1) {
    $('#' + prefix + '1').addClass('ledOn');
  }
}
